package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_bot;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_bot;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_botSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_botService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[邮件机器人] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_botServiceImpl implements IMail_botService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_botClientService mail_botClientService;

    @Override
    public boolean update(Mail_bot mail_bot) {
        boolean bOk = false ;
        Imail_bot clientModel = this.convert2Model(mail_bot,null);
        mail_botClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_bot) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public Mail_bot get(Integer id) {
        Imail_bot clientModel = mail_botClientService.createModel();
        clientModel.setId(id);
        mail_botClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_bot get(Mail_bot mail_bot) {
        Mail_bot retmail_bot = this.get(mail_bot.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_bot.getClass(), mail_bot.getClass(), false);
        copier.copy(retmail_bot, mail_bot, null);
        return mail_bot ;
    }

    @Override
    public boolean create(Mail_bot mail_bot) {
        boolean bOk = false ;
        Imail_bot clientModel = this.convert2Model(mail_bot,null);
        mail_botClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_bot);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_botClientService.removeBatch(mail_bots) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_bot clientModel = mail_botClientService.createModel();
        clientModel.setId(id);
        mail_botClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Mail_bot> mail_bots, int batchSize) {

                        //mail_botClientService.createBatch(mail_bots,batchSize) ;

    }

    @Override
    public void updateBatch(List<Mail_bot> mail_bots, int batchSize) {

        //mail_botClientService.updateBatch(mail_bots,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_bot> searchDefault(Mail_botSearchContext context) {
        List<Mail_bot> list = new ArrayList<Mail_bot>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_bot> clientModelList = mail_botClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_bot clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_bot>(list, context.getPageable(), clientModelList.getTotalElements());
    }




    public Imail_bot convert2Model(Mail_bot domain , Imail_bot model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_botClientService.createModel() ;
        }

         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
        return model ;
    }

    public Mail_bot convert2Domain( Imail_bot model ,Mail_bot domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_bot();
        }

        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        return domain ;
    }


}
